Page({
  data: {
    records: []
  },
  async onShow() {
    const openid = (await wx.cloud.callFunction({ name: 'getOpenId' })).result.openid;
    const db = wx.cloud.database();
    // 查trades集合
    const res = await db.collection('trades')
      .where({ openid })
      .orderBy('time', 'desc')
      .get();
    // 格式化：type 显示为中文，金额保留2位小数
    const records = res.data.map(item => ({
      ...item,
      typeText: item.type === 'buy' ? '买入' : '卖出',
      amount: Number(item.amount).toFixed(2),
      price: Number(item.price).toFixed(2)
    }));
    this.setData({ records });
  }
});
